package com.bulls.demo.hrmsdemo.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.bulls.demo.hrmsdemo.constants.Constants;
import com.bulls.demo.hrmsdemo.db.Vendor;
import com.bulls.demo.hrmsdemo.form.VendorMasterForm;
import com.bulls.demo.hrmsdemo.helper.VendorMasterHelper;
import com.bulls.demo.hrmsdemo.service.VendorMasterService;

public class VendorMasterAction extends DispatchAction {
	public ActionForward execute(ActionMapping actionMapping,ActionForm actionForm,
			 HttpServletRequest request,HttpServletResponse response)throws Exception{
		System.out.println("action------------>");
		String forwardPath = "failure";
		VendorMasterForm form = (VendorMasterForm)actionForm;
		String command = request.getParameter("cmd");
		System.out.println("cmd"+command);
		if(Constants.CMD_OPEN.equals(command)){
			forwardPath = executeOpen(request,response,form);
		}else if(Constants.CMD_SAVE.equalsIgnoreCase(form.getMethod())){
			forwardPath = executeSave(request,response,form);
		}else if(Constants.CMD_SEARCH.equalsIgnoreCase(form.getMethod())){
			forwardPath = executeSearch(request,response,form);
		}else if(Constants.CMD_DELETE.equalsIgnoreCase(form.getMethod())){
			forwardPath = executeDelete(request,response,form);
		}
		return actionMapping.findForward(forwardPath);
	}

	private String executeOpen(HttpServletRequest request,
			HttpServletResponse response, VendorMasterForm form) {
		
		return "open";
		// TODO Auto-generated method stub
		
	}
	
	private String executeSave(HttpServletRequest request,
			HttpServletResponse response, VendorMasterForm form) {
		Vendor vendor = new Vendor();
		VendorMasterHelper.toDomainObject(form, vendor);
		vendor = getVendorMasterService().save(vendor);
		request.setAttribute("vendor", vendor);
		return "success";
		// TODO Auto-generated method stub
		
	}
	
	private String executeSearch(HttpServletRequest request,
			HttpServletResponse response, VendorMasterForm form) {
		Vendor vendor = new Vendor();
		vendor = getVendorMasterService().search(form.getVendorid());
		return "success";
		// TODO Auto-generated method stub
		
	}
	
	private String executeDelete(HttpServletRequest request,
			HttpServletResponse response, VendorMasterForm form) {
		
		return "open";
		// TODO Auto-generated method stub
		
	}
	
	
	public VendorMasterService getVendorMasterService(){
		VendorMasterService vendorMasterService = new VendorMasterService();
		return vendorMasterService;
	}
}
